package com.mzkj.wanjulaile.mobile.api.modules.appointment.service;

import com.google.common.collect.Lists;
import com.mzkj.common.utils.StringUtils;
import com.mzkj.wanjulaile.mobile.api.modules.account.entity.Account;
import com.mzkj.wanjulaile.mobile.api.modules.appointment.entity.AppointmentCollection;
import com.mzkj.wanjulaile.mobile.api.modules.appointment.mapper.AppointmentCollectionMapper;
import com.mzkj.wanjulaile.mobile.api.modules.shoppingCart.entity.ShoppingCart;
import com.mzkj.wanjulaile.mobile.api.modules.shoppingCart.mapper.ShoppingCartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
public class AppointmentCollectionServiceImpl implements AppointmentCollectionService {

	@Autowired
	private AppointmentCollectionMapper appointmentCollectionMapper;

	@Autowired
	private ShoppingCartMapper shoppingCartMapper;
	
	public AppointmentCollection get(String id) {
		AppointmentCollection appointmentCollection = appointmentCollectionMapper.get(id);
		return appointmentCollection;
	}
	
	public List<AppointmentCollection> findList(AppointmentCollection appointmentCollection) {
		return appointmentCollectionMapper.findList(appointmentCollection);
	}

	
	@Transactional(readOnly = false)
	public void delete(AppointmentCollection appointmentCollection) {
		appointmentCollectionMapper.delete(appointmentCollection);
	}

	@Transactional(readOnly = false)
	public void delete1(String id){
		AppointmentCollection appointmentCollection=appointmentCollectionMapper.get(id);
		//删除预约时,删除购物车
		if(AppointmentCollection.TYPE_APPOINTMENT.equals(appointmentCollection.getType())){
			List<String> toyIds= Lists.newArrayList();
			toyIds.add(appointmentCollection.getToy().getId());
			shoppingCartMapper.batchDelete(toyIds,appointmentCollection.getAccount().getId());
		}
		appointmentCollectionMapper.delete(appointmentCollection);
	}

	@Override
	@Transactional(readOnly = false)
	public void save(AppointmentCollection appointmentCollection) {
		if (StringUtils.isBlank(appointmentCollection.getId())){
			appointmentCollection.setIsNewRecord(true);
			appointmentCollection.preInsert();
			appointmentCollectionMapper.insert(appointmentCollection);
		}else{
			appointmentCollection.preUpdate();
			appointmentCollectionMapper.update(appointmentCollection);
		}
	}

	/**
	 * 加入收藏
	 * @param appointmentCollection
     */
	@Override
	@Transactional
	public void addAppColl(AppointmentCollection appointmentCollection) {
		if (StringUtils.isBlank(appointmentCollection.getId())){
			appointmentCollection.setIsNewRecord(true);
			appointmentCollection.preInsert();
			appointmentCollectionMapper.insert(appointmentCollection);
		}
	}

	/**
	 * 是否重复预约和收藏
	 * @param appointmentCollection
	 * @return
     */
    public List<AppointmentCollection> getExists(AppointmentCollection appointmentCollection){
    	return appointmentCollectionMapper.getExists(appointmentCollection);
	}

	/**
	 * 取消收藏
	 * @param appointmentCollection
     */
	@Transactional(readOnly = false)
	public void cancelCollect(AppointmentCollection appointmentCollection){
		appointmentCollectionMapper.cancelCollect(appointmentCollection);
	}

	@Transactional(readOnly = false)
	public void deleteByAccountIdAndToy(AppointmentCollection app) {
		appointmentCollectionMapper.deleteByAccountIdAndToy(app);
	}

//	/**
//	 * 查找预约玩具 有库存
//	 * @return
//     */
//	public List<AppointmentCollection> findAppointToyList() {
//		return appointmentCollectionMapper.findAppointToyList();
//	}

//	/**
//	 * 根据玩具id查询预约list
//	 * @param toyId
//	 * @return
//     */
//	public List<AppointmentCollection> findAppointListByToyId(String toyId) {
//		return appointmentCollectionMapper.findAppointListByToyId(toyId);
//	}
}